#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include "RingQueue.hpp"
#include"Task.hpp"

using namespace std;

void *Productor(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    int len=opers.size();
    while (true)
    {
        // 1.获取数据
        int data1 = rand() % 10 + 1;
        usleep(10);
        int data2 = rand() % 10;

        char op=opers[rand()%len];
        Task t(data1,data2,op);
        // 2.生产数据
        rq->Push(t);
        cout << "Productor data done, task is :" << t.GetTask() << endl;
        sleep(2);
    }
    return nullptr;
}

void *Consumer(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(args);
    while (true)
    {
        // 1.消费数据
        Task t;
        rq->Pop(&t);
        cout << "Consumer get data, task is" << t.GetTask()<<"result: "<<t.GetResult()<< endl;
        sleep(2);
        // 2.处理数据
    }
    return nullptr;
}

int main()
{
    srand(time(nullptr) ^ getpid());

    RingQueue<Task> *rq = new RingQueue<Task>();

    pthread_t c[5], p[3];
    for (int i = 0; i < 3; i++)
    {
        pthread_create(p + i, nullptr, Productor, rq);
    }
    for (int i = 0; i < 5; i++)
    {
        pthread_create(c + i, nullptr, Consumer, rq);
    }
    for (int i = 0; i < 3; i++)
    {
        pthread_join(p[i], nullptr);
    }
    for (int i = 0; i < 5; i++)
    {
        pthread_join(c[i], nullptr);
    }
}